#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
@File Name  : Chrome
@Author     : LeeCQ
@Date-Time  : 2020/2/12 21:26

chrome设置集成

"""

from selenium.webdriver import Chrome, ChromeOptions
import logging
import sys

logger = logging.getLogger("logger")  # 创建实例
formatter = logging.Formatter("[%(asctime)s] < %(funcName)s: %(thread)d > [%(levelname)s] %(message)s")
# 终端日志
consle_handler = logging.StreamHandler(sys.stdout)
consle_handler.setFormatter(formatter)  # 日志文件的格式
logger.setLevel(logging.DEBUG)  # 设置日志文件等级
#
__all__ = ['chrome', 'Chrome']


def chrome(executable_path='chromedriver.exe',
           is_headless: bool = False, headless=None,
           is_maximized: bool = False, maximized=None,
           is_incognito: bool = False, incognito=None,
           use_js: bool = True, js: bool = None,
           custom_ua: str = None, ua: str = None,
           display_pic: bool = True, pic=True,
           display_notifications: bool = True, notification=None,
           **kwargs
           ):
    """参数说明：

    :param ua:
    :param js:
    :param maximized: 窗口最大化
    :param notification: 通知
    :param pic: 显示图片
    :param headless: 最小化
    :param incognito: 影身
    :param executable_path: chromedriver可执行程序的位置。
    :param is_headless: 隐藏窗口
    :param is_maximized: 窗口最大化
    :param is_incognito: 使用无痕模式
    :param use_js: 是否使用JS
    :param custom_ua: 使用给定的浏览器UA
    :param display_pic: 是否加载图片
    :param display_notifications: 是否加载弹窗
    """
    is_headless = is_headless if headless is None else headless
    is_maximized = is_maximized if maximized is None else maximized
    is_incognito = is_incognito if incognito is None else incognito
    custom_ua = custom_ua if ua is None else ua
    use_js = use_js if js is None else js
    display_pic = display_pic if pic is None else pic
    display_notifications = display_notifications if notification else notification

    __opt = _option(headless=is_headless,
                    maximized=is_maximized,
                    js=use_js, ua=custom_ua,
                    incognito=is_incognito,
                    pic=display_pic,
                    notifications=display_notifications, **kwargs
                    )
    browser = Chrome(executable_path=executable_path,
                     options=__opt
                     )
    return browser


def _option(headless, maximized, incognito, js, ua, pic, notifications, **kwargs):
    option = ChromeOptions()
    # 设置
    _pre = dict()

    option.headless = headless
    if maximized is True: option.add_argument('--start-maximized')  # 最大化
    if incognito is True: option.add_argument('–-incognito')  # 基本没什么用
    if ua: option.add_argument(f'user-agent="{ua}"')  # 设置UA
    if js is False: _pre.update({'javascript': 2})  # 设置JS
    if pic is False: _pre.update({'images': 2})  # 设置pic
    if notifications is False: _pre.update({'notifications': 2})  # 设置通知
    # 可拓展
    if 'argument' in kwargs.keys():
        option.add_argument(kwargs.get('argument') if isinstance(kwargs.get('argument'), str) else '')
    if 'arguments' in kwargs.keys():
        for _ in kwargs.get('arguments') if isinstance(kwargs.get('arguments'), str) else []:
            option.add_argument(_ if isinstance(_, str) else '')

    option.add_experimental_option('prefs', {'profile.default_content_setting_values': _pre})
    return option


if __name__ == '__main__':
    logger.addHandler(consle_handler)  # 添加控制台
    b = chrome(  # pic=False, use_js=False,
        ua="MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"
    )
    baidu = 'http://baidu.com'
    b.get(baidu)
# options.add_argument('user-agent="MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"')
